package org.sardine.aop;

import java.util.ArrayList;
import java.util.List;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.apache.log4j.Logger;
import org.sardine.aop.advice.AfterAdvice;
import org.sardine.aop.advice.AroundInterceptor;
import org.sardine.aop.advice.BeforeAdvice;
import org.sardine.util.Wildcard;

public class ProxySupport {
	
	private final Logger log = Logger.getLogger(ProxySupport.class);
	private Object target;
	private Class targetClass;
	private List<Class> targetInterfaces = new ArrayList<Class>();
	private List<Advice> interceptors = new ArrayList<Advice>();
	//private List<String> targetMethodNames = new ArrayList<String>();
	//private List<String> regexs = new ArrayList<String>();
	//private List<String> fullyQualifiedMethodNamePatterns = new ArrayList<String>();
	private List<String> methodNamePatterns = new ArrayList<String>();
	boolean useFullyQualifiedName = false;
	private boolean cglib = false;
	
	public Class[] getProxiedTargetInterfaces(){
		return (Class[])targetInterfaces.toArray(new Class[targetInterfaces.size()]);
	}

	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}

	public List<Class> getTargetInterfaces() {
		return targetInterfaces;
	}

	public boolean isCglib() {
		return cglib;
	}

	public void setTargetInterfaces(List<Class> targetInterfaces) {
		this.targetInterfaces = targetInterfaces;
	}

	public void setCglib(boolean cglib) {
		this.cglib = cglib;
	}

	public List<Advice> getInterceptors() {
		return interceptors;
	}

	public void setInterceptors(List<Advice> interceptors) {
		for(Advice advice : interceptors){
			addInterceptor(advice);
		}
	}
	
	public Class getTargetClass() {
		return targetClass;
	}

	public void setTargetClass(Class targetClass) {
		this.targetClass = targetClass;
	}

	public void addInterceptor(Advice interceptor){
		
		if(interceptor instanceof BeforeAdvice || interceptor instanceof AfterAdvice){
			this.interceptors.add(new AroundInterceptor(interceptor));
		
		}else if(interceptor instanceof MethodInterceptor){
			this.interceptors.add(interceptor);
			
		}else{
			throw new IllegalArgumentException(interceptor+" 可能不是合法的参数，它必须要实现BeforeAdivce接口、AfterAdvice接口或MethodInterceptor接口");
		}
	}

	public List<String> getMethodNamePatterns() {
		return methodNamePatterns;
	}

	public void setMethodNamePatterns(List<String> methodNamePatterns) {
		this.methodNamePatterns = methodNamePatterns;
	}

	public boolean isUseFullyQualifiedName() {
		return useFullyQualifiedName;
	}

	public void setUseFullyQualifiedName(boolean useFullyQualifiedName) {
		this.useFullyQualifiedName = useFullyQualifiedName;
	}

	public boolean matchMethodNamePattern(String className,String methodName){
		
		String fullMethodName = null;
		
		if(useFullyQualifiedName){
			fullMethodName = className + "." + methodName;
		}else{
			fullMethodName = methodName;
		}
		
		log.debug("is calling "+fullMethodName);
		boolean result = false;
		
		//List<String> methodNamePatterns = support.getMethodNamePatterns();
		for(String pattern : this.methodNamePatterns){
			result = Wildcard.match(fullMethodName, pattern);
			log.debug("regex is "+ pattern +" and current result is "+result);
			if(result){
				return result;
			}
		}
		return result;
		
	}
	
	

}
